
 var service = require('./service');

var express = require('express');

// 1. 创建一个路由容器
var router = express.Router();


router.get('/main', function (req, res) {
  service.find(function (err, list) {
    res.render('main.html', {
      list: list
    })
  })
});


router.get('/login', function (req, res) {
  res.render('login.html')
})

router.post('/login', function (req, res) {
  service.login(req.body, function (data) {
    if (data.code !=0) {
      return res.status(500).send('Server error.')
    }
    res.redirect('/main')
  })
});
 router.get('/addlvyou', function (req, res) {
   res.render('addLvYou.html')
 });
 router.post('/addlvyou', function (req, res) {
   service.saveLvYou(req.body, function (err) {
     res.redirect('/main')
   })
 });
router.get('/add', function (req, res) {
  res.render('add.html')
});


router.post('/add', function (req, res) {
  service.save(req.body, function (err) {
    if (err) {
      return res.status(500).send('Server error.')
    }
    res.redirect('/login')
  })
})

router.get('/detail', function (req, res) {
  service.detail(req.query.id, function (lvyou) {
    res.render('detail.html', {
      lvyou: lvyou
    })
  })
})

router.get('/delete', function (req, res) {
  service.deleteById(req.query.id, function (err) {
    res.redirect('/main');
  })
})






// 3. 把 router 导出
module.exports = router

